/**
 * KeypadConsole.java the main file to start S|D
 * 
 * Version 1.0
 * 
 * Copyright (c) 2011, Brij Mohan Lal Srivastava, Leonidas
 * 
 * @author Brij Mohan Lal Srivastava
 */
package com.leonidas.speeddial;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class KeypadConsole extends Activity {

	public static final String SD_DATA = "PrefsFile";

	private static final int CONTACT_PICKER_RESULT_1 = 1001;
	private static final int CONTACT_PICKER_RESULT_2 = 1002;
	private static final int CONTACT_PICKER_RESULT_3 = 1003;
	private static final int CONTACT_PICKER_RESULT_4 = 1004;
	private static final int CONTACT_PICKER_RESULT_5 = 1005;
	private static final int CONTACT_PICKER_RESULT_6 = 1006;
	private static final int CONTACT_PICKER_RESULT_7 = 1007;
	private static final int CONTACT_PICKER_RESULT_8 = 1008;
	private static final int CONTACT_PICKER_RESULT_9 = 1009;

	boolean user1, user2, user3, user4, user5, user6, user7, user8, user9,
			first_time;

	AlertDialog alert;

	ImageButton sd_user_1, sd_user_2, sd_user_3, sd_user_4, sd_user_5,
			sd_user_6, sd_user_7, sd_user_8, sd_user_9;

	/** Called when the activity is first created. */

	protected Dialog onCreateDialog(int id, final String data_param) {
		switch (id) {
		case 0:
			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.add)
					.setTitle("Not assigned. Assign it now?")
					.setPositiveButton("Yeah",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									Toast.makeText(getBaseContext(),
											"Pick a Contact!",
											Toast.LENGTH_SHORT).show();
									Intent intent = new Intent(
											Intent.ACTION_PICK,
											Contacts.CONTENT_URI);
									// intent.putExtra("com.leonidas.speeddial.sd_num_ref",
									// data_param+"");
									if (data_param == "sd_data_1")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_1);
									if (data_param == "sd_data_2")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_2);
									if (data_param == "sd_data_3")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_3);
									if (data_param == "sd_data_4")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_4);
									if (data_param == "sd_data_5")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_5);
									if (data_param == "sd_data_6")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_6);
									if (data_param == "sd_data_7")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_7);
									if (data_param == "sd_data_8")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_8);
									if (data_param == "sd_data_9")
										startActivityForResult(intent,
												CONTACT_PICKER_RESULT_9);
								}
							})
					.setNegativeButton("Not now",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									/*
									 * Toast.makeText(getBaseContext(),
									 * "Cancel clicked!",
									 * Toast.LENGTH_SHORT).show();
									 */
								}
							}).create();
		case 1:

			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);

			String uid = settings.getString(data_param, "null");

			Uri myPerson = Uri.withAppendedPath(Contacts.CONTENT_URI, uid);

			Cursor cur = managedQuery(myPerson, null, null, null, null);
			cur.moveToFirst();
			int NameColumn = cur.getColumnIndex(Contacts.DISPLAY_NAME);
			String ConName = cur.getString(NameColumn);

			return new AlertDialog.Builder(this)
					.setIcon(R.drawable.remove)
					.setTitle("Remove " + ConName + " ?")
					.setPositiveButton("Yeah",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									/*
									 * Toast.makeText(getBaseContext(),
									 * "Pick a Contact!",
									 * Toast.LENGTH_SHORT).show();
									 */
									removeThis(data_param);
								}
							})
					.setNegativeButton("No Please!",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									/*
									 * Toast.makeText(getBaseContext(),
									 * "Nothing removed!",
									 * Toast.LENGTH_SHORT).show();
									 */
								}
							}).create();
		}
		return null;
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);

		switch (reqCode) {
		case (CONTACT_PICKER_RESULT_1):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();
					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_1", con_id);
					editor.putBoolean("sd1", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_2):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_2", con_id);
					editor.putBoolean("sd2", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_3):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_3", con_id);
					editor.putBoolean("sd3", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_4):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_4", con_id);
					editor.putBoolean("sd4", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_5):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_5", con_id);
					editor.putBoolean("sd5", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_6):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_6", con_id);
					editor.putBoolean("sd6", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_7):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_7", con_id);
					editor.putBoolean("sd7", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_8):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_8", con_id);
					editor.putBoolean("sd8", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		case (CONTACT_PICKER_RESULT_9):
			if (resultCode == Activity.RESULT_OK) {

				Uri contactData = data.getData();

				ContentResolver cr = getContentResolver();
				Cursor c = cr.query(contactData, null, null, null, null);

				if (c.moveToFirst()) {
					String con_name = c.getString(c
							.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
					String con_id = c.getString(c
							.getColumnIndexOrThrow(Contacts._ID));
					// TODO Whatever you want to do with the selected contact
					// data.
					Toast.makeText(getBaseContext(), con_name + " Selected",
							Toast.LENGTH_SHORT).show();

					SharedPreferences settings = getSharedPreferences(SD_DATA,
							Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("sd_data_9", con_id);
					editor.putBoolean("sd9", true);
					editor.commit();
					refreshAll();
				}
			}
			break;
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Checking if all 9 contacts are set:

		SharedPreferences settings = getSharedPreferences(SD_DATA,
				Context.MODE_PRIVATE);
		user1 = settings.getBoolean("sd1", false);
		user2 = settings.getBoolean("sd2", false);
		user3 = settings.getBoolean("sd3", false);
		user4 = settings.getBoolean("sd4", false);
		user5 = settings.getBoolean("sd5", false);
		user6 = settings.getBoolean("sd6", false);
		user7 = settings.getBoolean("sd7", false);
		user8 = settings.getBoolean("sd8", false);
		user9 = settings.getBoolean("sd9", false);
		first_time = settings.getBoolean("first", false);

		// Action to take when opened first time
		if (first_time == false) {
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean("first", true);
			editor.commit();
			Intent first_dialog = new Intent(Intent.ACTION_CALL_BUTTON);
			startActivity(first_dialog);
		}

		// Action taken for user 1
		if (user1 == false) {
			sd_user_1 = (ImageButton) findViewById(R.id.sd_1);
			sd_user_1.setImageResource(R.drawable.addcontact);
			sd_user_1.setOnClickListener(add_user1);
		} else {
			String uid = settings.getString("sd_data_1", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_1 = (ImageButton) findViewById(R.id.sd_1);
				sd_user_1.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_1);
				sd_user_1.setOnClickListener(add_user1);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd1", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#1 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_1 = (ImageButton) findViewById(R.id.sd_1);
				sd_user_1.setImageResource(R.drawable.addcontact);
				sd_user_1.setOnClickListener(add_user1);
			}
		}
		// Action taken for user 2
		if (user2 == false) {
			sd_user_2 = (ImageButton) findViewById(R.id.sd_2);
			sd_user_2.setImageResource(R.drawable.addcontact);
			sd_user_2.setOnClickListener(add_user2);
		} else {
			String uid = settings.getString("sd_data_2", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_2 = (ImageButton) findViewById(R.id.sd_2);
				sd_user_2.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_2);
				sd_user_2.setOnClickListener(add_user2);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd2", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#2 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_2 = (ImageButton) findViewById(R.id.sd_2);
				sd_user_2.setImageResource(R.drawable.addcontact);
				sd_user_2.setOnClickListener(add_user2);
			}
		}
		// Action taken for user 3
		if (user3 == false) {
			sd_user_3 = (ImageButton) findViewById(R.id.sd_3);
			sd_user_3.setImageResource(R.drawable.addcontact);
			sd_user_3.setOnClickListener(add_user3);
		} else {
			String uid = settings.getString("sd_data_3", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_3 = (ImageButton) findViewById(R.id.sd_3);
				sd_user_3.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_3);
				sd_user_3.setOnClickListener(add_user3);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd3", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#3 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_3 = (ImageButton) findViewById(R.id.sd_3);
				sd_user_3.setImageResource(R.drawable.addcontact);
				sd_user_3.setOnClickListener(add_user3);
			}
		}
		// Action taken for user 4
		if (user4 == false) {
			sd_user_4 = (ImageButton) findViewById(R.id.sd_4);
			sd_user_4.setImageResource(R.drawable.addcontact);
			sd_user_4.setOnClickListener(add_user4);
		} else {
			String uid = settings.getString("sd_data_4", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_4 = (ImageButton) findViewById(R.id.sd_4);
				sd_user_4.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_4);
				sd_user_4.setOnClickListener(add_user4);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd4", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#4 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_4 = (ImageButton) findViewById(R.id.sd_4);
				sd_user_4.setImageResource(R.drawable.addcontact);
				sd_user_4.setOnClickListener(add_user4);
			}
		}
		// Action taken for user 5
		if (user5 == false) {
			sd_user_5 = (ImageButton) findViewById(R.id.sd_5);
			sd_user_5.setImageResource(R.drawable.addcontact);
			sd_user_5.setOnClickListener(add_user5);
		} else {
			String uid = settings.getString("sd_data_5", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_5 = (ImageButton) findViewById(R.id.sd_5);
				sd_user_5.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_5);
				sd_user_5.setOnClickListener(add_user5);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd5", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#5 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_5 = (ImageButton) findViewById(R.id.sd_5);
				sd_user_5.setImageResource(R.drawable.addcontact);
				sd_user_5.setOnClickListener(add_user5);
			}
		}
		// Action taken for user 6
		if (user6 == false) {
			sd_user_6 = (ImageButton) findViewById(R.id.sd_6);
			sd_user_6.setImageResource(R.drawable.addcontact);
			sd_user_6.setOnClickListener(add_user6);
		} else {
			String uid = settings.getString("sd_data_6", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_6 = (ImageButton) findViewById(R.id.sd_6);
				sd_user_6.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_6);
				sd_user_6.setOnClickListener(add_user6);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd6", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#6 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_6 = (ImageButton) findViewById(R.id.sd_6);
				sd_user_6.setImageResource(R.drawable.addcontact);
				sd_user_6.setOnClickListener(add_user6);
			}
		}
		// Action taken for user 7
		if (user7 == false) {
			sd_user_7 = (ImageButton) findViewById(R.id.sd_7);
			sd_user_7.setImageResource(R.drawable.addcontact);
			sd_user_7.setOnClickListener(add_user7);
		} else {
			String uid = settings.getString("sd_data_7", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_7 = (ImageButton) findViewById(R.id.sd_7);
				sd_user_7.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_7);
				sd_user_7.setOnClickListener(add_user7);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd7", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#1 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_7 = (ImageButton) findViewById(R.id.sd_7);
				sd_user_7.setImageResource(R.drawable.addcontact);
				sd_user_7.setOnClickListener(add_user7);
			}
		}
		// Action taken for user 8
		if (user8 == false) {
			sd_user_8 = (ImageButton) findViewById(R.id.sd_8);
			sd_user_8.setImageResource(R.drawable.addcontact);
			sd_user_8.setOnClickListener(add_user8);
		} else {
			String uid = settings.getString("sd_data_8", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_8 = (ImageButton) findViewById(R.id.sd_8);
				sd_user_8.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_8);
				sd_user_8.setOnClickListener(add_user8);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd8", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#8 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_8 = (ImageButton) findViewById(R.id.sd_8);
				sd_user_8.setImageResource(R.drawable.addcontact);
				sd_user_8.setOnClickListener(add_user8);
			}
		}
		// Action taken for user 9
		if (user9 == false) {
			sd_user_9 = (ImageButton) findViewById(R.id.sd_9);
			sd_user_9.setImageResource(R.drawable.addcontact);
			sd_user_9.setOnClickListener(add_user9);
		} else {
			String uid = settings.getString("sd_data_9", "null");
			Cursor cur = getContentResolver().query(
					ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			if (uid != "null" && (cur.getCount() != 0)) {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_9 = (ImageButton) findViewById(R.id.sd_9);
				sd_user_9.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_9);
				sd_user_9.setOnClickListener(add_user9);
			} else {
				SharedPreferences.Editor editor = settings.edit();
				editor.putBoolean("sd9", false);
				editor.commit();
				Toast.makeText(getBaseContext(), "Contact for SD#9 deleted!",
						Toast.LENGTH_SHORT).show();
				sd_user_9 = (ImageButton) findViewById(R.id.sd_9);
				sd_user_9.setImageResource(R.drawable.addcontact);
				sd_user_9.setOnClickListener(add_user9);
			}
		}
	}

	private View lastView = null;

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.sdcontext, menu);

		// Getting User data who was clicked
		String user_data = (String) v.getContentDescription();
		SharedPreferences settings = getSharedPreferences(SD_DATA,
				Context.MODE_PRIVATE);
		String uid = settings.getString(user_data, "null");
		ContentResolver cr = getContentResolver();

		String ConEmail = "not set";
		String ConNum = "null";
		String ConName = "null";

		if (uid != "null") {

			Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			cur.moveToFirst();
			if (Integer
					.parseInt(cur.getString(cur
							.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
				Cursor pCur = cr.query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
								+ " = ?", new String[] { uid }, null);
				// Do something with phones
				pCur.moveToFirst();
				ConNum = pCur
						.getString(pCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
				ConName = pCur
						.getString(pCur
								.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				pCur.close();
			}
			ConName = cur.getString(cur
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			Cursor emailCur = cr.query(
					ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
					new String[] { uid }, null);
			if (emailCur.getCount() > 0) {
				emailCur.moveToFirst();
				ConEmail = emailCur
						.getString(emailCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
			}
			emailCur.close();

			String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
					+ ContactsContract.Data.MIMETYPE + " = ?";
			String[] imWhereParams = new String[] { uid,
					ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
			Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, null,
					imWhere, imWhereParams, null);
			String imName = "not set";
			String imType = null;
			if (imCur.moveToFirst()) {
				imName = imCur
						.getString(imCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));

				imType = imCur
						.getString(imCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
			}
			imCur.close();

			if (ConNum == "null") {
				menu.findItem(R.id.call).setVisible(false);
				menu.findItem(R.id.sms).setVisible(false);
				menu.findItem(R.id.mms).setVisible(false);
			}
			if (ConEmail == "not set") {
				menu.findItem(R.id.email).setVisible(false);
			}
			if (imName == "not set") {
				menu.findItem(R.id.chat).setVisible(false);
			}
			// Applying data
			menu.setHeaderTitle(ConName);
			menu.setHeaderIcon(R.drawable.sdlauncher);
			menu.findItem(R.id.call).setTitle("Call this contact");
			menu.findItem(R.id.sms).setTitle("SMS this contact");
			menu.findItem(R.id.mms).setTitle("MMS this contact");
			menu.findItem(R.id.fb).setTitle("View Facebook Profile");
			menu.findItem(R.id.email).setTitle("Email to [" + ConEmail + "]");
			menu.findItem(R.id.chat).setTitle("Chat with [" + imName + "]");
			lastView = v;
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();

		SharedPreferences settings = getSharedPreferences(SD_DATA,
				Context.MODE_PRIVATE);
		String phoneNumber = null;
		// String perName = null;
		String ConEmail = null;
		String imName = null;
		String imType = null;
		String sd_id = ((ImageButton) lastView).getContentDescription()
				.toString();
		String uid = settings.getString(sd_id, "null");
		if (uid != "null") {
			ContentResolver cr = getContentResolver();

			Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
					ContactsContract.Contacts._ID + " = ?",
					new String[] { uid }, null);
			cur.moveToFirst();
			if (Integer
					.parseInt(cur.getString(cur
							.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
				Cursor pCur = cr.query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
								+ " = ?", new String[] { uid }, null);
				// Do something with phones
				pCur.moveToFirst();
				phoneNumber = pCur
						.getString(pCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
				pCur.close();
			}

			Cursor emailCur = cr.query(
					ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
					ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
					new String[] { uid }, null);
			ConEmail = "Not Assigned";
			if (emailCur.getCount() > 0) {
				emailCur.moveToFirst();
				ConEmail = emailCur
						.getString(emailCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
			}
			emailCur.close();

			String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
					+ ContactsContract.Data.MIMETYPE + " = ?";
			String[] imWhereParams = new String[] { uid,
					ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
			Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, null,
					imWhere, imWhereParams, null);
			if (imCur.moveToFirst()) {
				imName = imCur
						.getString(imCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));

				imType = imCur
						.getString(imCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
			}
			imCur.close();
		}
		// long id = info.id;
		switch (item.getItemId()) {
		case R.id.call:
			callThis(uid);
			return true;
		case R.id.sms:
			smsThis(uid);
			return true;
		case R.id.mms:
			mmsThis(uid);
			return true;
		case R.id.fb:
			Intent view_fb = new Intent(Intent.ACTION_VIEW);
			view_fb.setData(Uri.parse("http://www.facebook.com"));
			startActivity(view_fb);
			return true;
		case R.id.email:
			Intent email_user = new Intent(Intent.ACTION_SEND);
			email_user.setType("plain/text");
			email_user.putExtra(Intent.EXTRA_EMAIL, new String[] { ConEmail });
			email_user.putExtra(Intent.EXTRA_SUBJECT, "Note");
			email_user.putExtra(Intent.EXTRA_TEXT, "--Sent from S|D");
			startActivity(Intent.createChooser(email_user,
					"Choose e-mail clients-"));
			return true;
		case R.id.chat:
			/*
			 * Intent chat_user = new
			 * Intent(Intent.ACTION_GTALK_SERVICE_CONNECTED);
			 * chat_user.putExtra("target", imName); startActivity(chat_user);
			 */
			Uri imUri = new Uri.Builder().scheme("imto").authority("gtalk")
					.appendPath(imName).build();
			Intent chat_user = new Intent(Intent.ACTION_SENDTO, imUri);
			startActivity(chat_user);
			return true;
		case R.id.view:
			Uri user_det = Uri.parse("content://contacts/people/" + uid);
			Intent view_user = new Intent(Intent.ACTION_VIEW, user_det);
			startActivity(view_user);
			return true;
		case R.id.remove:
			// removeThis(sd_id);
			onCreateDialog(1, sd_id).show();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public void onContextMenuClosed(Menu menu) {
		// We don't need it anymore
		lastView = null;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainoptions, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.android_dialer:
			Intent dial = new Intent(Intent.ACTION_DIAL);
			startActivity(dial);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void refreshAll() {
		SharedPreferences settings = getSharedPreferences(SD_DATA,
				Context.MODE_PRIVATE);
		user1 = settings.getBoolean("sd1", false);
		user2 = settings.getBoolean("sd2", false);
		user3 = settings.getBoolean("sd3", false);
		user4 = settings.getBoolean("sd4", false);
		user5 = settings.getBoolean("sd5", false);
		user6 = settings.getBoolean("sd6", false);
		user7 = settings.getBoolean("sd7", false);
		user8 = settings.getBoolean("sd8", false);
		user9 = settings.getBoolean("sd9", false);
		// Action taken for user 1
		if (user1 == false) {
			sd_user_1 = (ImageButton) findViewById(R.id.sd_1);
			sd_user_1.setImageResource(R.drawable.addcontact);
			sd_user_1.setOnClickListener(add_user1);
		} else {
			String uid = settings.getString("sd_data_1", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_1 = (ImageButton) findViewById(R.id.sd_1);
				sd_user_1.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_1);
				sd_user_1.setOnClickListener(add_user1);
			}
		}
		// Action taken for user 2
		if (user2 == false) {
			sd_user_2 = (ImageButton) findViewById(R.id.sd_2);
			sd_user_2.setImageResource(R.drawable.addcontact);
			sd_user_2.setOnClickListener(add_user2);
		} else {
			String uid = settings.getString("sd_data_2", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_2 = (ImageButton) findViewById(R.id.sd_2);
				sd_user_2.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_2);
				sd_user_2.setOnClickListener(add_user2);
			}
		}
		// Action taken for user 3
		if (user3 == false) {
			sd_user_3 = (ImageButton) findViewById(R.id.sd_3);
			sd_user_3.setImageResource(R.drawable.addcontact);
			sd_user_3.setOnClickListener(add_user3);
		} else {
			String uid = settings.getString("sd_data_3", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_3 = (ImageButton) findViewById(R.id.sd_3);
				sd_user_3.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_3);
				sd_user_3.setOnClickListener(add_user3);
			}
		}
		// Action taken for user 4
		if (user4 == false) {
			sd_user_4 = (ImageButton) findViewById(R.id.sd_4);
			sd_user_4.setImageResource(R.drawable.addcontact);
			sd_user_4.setOnClickListener(add_user4);
		} else {
			String uid = settings.getString("sd_data_4", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_4 = (ImageButton) findViewById(R.id.sd_4);
				sd_user_4.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_4);
				sd_user_4.setOnClickListener(add_user4);
			}
		}
		// Action taken for user 5
		if (user5 == false) {
			sd_user_5 = (ImageButton) findViewById(R.id.sd_5);
			sd_user_5.setImageResource(R.drawable.addcontact);
			sd_user_5.setOnClickListener(add_user5);
		} else {
			String uid = settings.getString("sd_data_5", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_5 = (ImageButton) findViewById(R.id.sd_5);
				sd_user_5.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_5);
				sd_user_5.setOnClickListener(add_user5);
			}
		}
		// Action taken for user 6
		if (user6 == false) {
			sd_user_6 = (ImageButton) findViewById(R.id.sd_6);
			sd_user_6.setImageResource(R.drawable.addcontact);
			sd_user_6.setOnClickListener(add_user6);
		} else {
			String uid = settings.getString("sd_data_6", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_6 = (ImageButton) findViewById(R.id.sd_6);
				sd_user_6.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_6);
				sd_user_6.setOnClickListener(add_user6);
			}
		}
		// Action taken for user 7
		if (user7 == false) {
			sd_user_7 = (ImageButton) findViewById(R.id.sd_7);
			sd_user_7.setImageResource(R.drawable.addcontact);
			sd_user_7.setOnClickListener(add_user7);
		} else {
			String uid = settings.getString("sd_data_7", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_7 = (ImageButton) findViewById(R.id.sd_7);
				sd_user_7.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_7);
				sd_user_7.setOnClickListener(add_user7);
			}
		}
		// Action taken for user 8
		if (user8 == false) {
			sd_user_8 = (ImageButton) findViewById(R.id.sd_8);
			sd_user_8.setImageResource(R.drawable.addcontact);
			sd_user_8.setOnClickListener(add_user8);
		} else {
			String uid = settings.getString("sd_data_8", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_8 = (ImageButton) findViewById(R.id.sd_8);
				sd_user_8.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_8);
				sd_user_8.setOnClickListener(add_user8);
			}
		}
		// Action taken for user 9
		if (user9 == false) {
			sd_user_9 = (ImageButton) findViewById(R.id.sd_9);
			sd_user_9.setImageResource(R.drawable.addcontact);
			sd_user_9.setOnClickListener(add_user9);
		} else {
			String uid = settings.getString("sd_data_9", "null");
			if (uid != "null") {
				Uri uri = ContentUris.withAppendedId(People.CONTENT_URI,
						Long.parseLong(uid));
				Bitmap bitmap = People.loadContactPhoto(getBaseContext(), uri,
						R.drawable.defimg, null);
				sd_user_9 = (ImageButton) findViewById(R.id.sd_9);
				sd_user_9.setImageBitmap(bitmap);
				registerForContextMenu(sd_user_9);
				sd_user_9.setOnClickListener(add_user9);
			}
		}
		/*
		 * Toast.makeText(getBaseContext(), "refreshed!",
		 * Toast.LENGTH_SHORT).show();
		 */
	}

	public void removeThis(String user_data_val) {
		SharedPreferences settings = getSharedPreferences(SD_DATA,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		// editor.putString(user_data_val, "null");

		if (user_data_val.equalsIgnoreCase("sd_data_1")) {
			editor.putBoolean("sd1", false);
			sd_user_1 = (ImageButton) findViewById(R.id.sd_1);
			unregisterForContextMenu(sd_user_1);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_2")) {
			editor.putBoolean("sd2", false);
			sd_user_2 = (ImageButton) findViewById(R.id.sd_2);
			unregisterForContextMenu(sd_user_2);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_3")) {
			editor.putBoolean("sd3", false);
			sd_user_3 = (ImageButton) findViewById(R.id.sd_3);
			unregisterForContextMenu(sd_user_3);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_4")) {
			editor.putBoolean("sd4", false);
			sd_user_4 = (ImageButton) findViewById(R.id.sd_4);
			unregisterForContextMenu(sd_user_4);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_5")) {
			editor.putBoolean("sd5", false);
			sd_user_5 = (ImageButton) findViewById(R.id.sd_5);
			unregisterForContextMenu(sd_user_5);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_6")) {
			editor.putBoolean("sd6", false);
			sd_user_6 = (ImageButton) findViewById(R.id.sd_6);
			unregisterForContextMenu(sd_user_6);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_7")) {
			editor.putBoolean("sd7", false);
			sd_user_7 = (ImageButton) findViewById(R.id.sd_7);
			unregisterForContextMenu(sd_user_7);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_8")) {
			editor.putBoolean("sd8", false);
			sd_user_8 = (ImageButton) findViewById(R.id.sd_8);
			unregisterForContextMenu(sd_user_8);
		}

		if (user_data_val.equalsIgnoreCase("sd_data_9")) {
			editor.putBoolean("sd9", false);
			sd_user_9 = (ImageButton) findViewById(R.id.sd_9);
			unregisterForContextMenu(sd_user_9);
		}

		if (editor.commit()) {
			Toast.makeText(getBaseContext(), "Removed!", Toast.LENGTH_SHORT)
					.show();
		} else {
			Toast.makeText(getBaseContext(), "Not removed!", Toast.LENGTH_SHORT)
					.show();
		}
		refreshAll();

	}

	public void callThis(String user_id) {
		String pn = "null";
		boolean many = false;
		ContentResolver cr = getContentResolver();

		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
				ContactsContract.Contacts._ID + " = ?",
				new String[] { user_id }, null);
		cur.moveToFirst();
		if (Integer.parseInt(cur.getString(cur
				.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
			Cursor pCur = cr.query(Data.CONTENT_URI, new String[] { Data._ID,
					Phone.NUMBER, Phone.TYPE, Phone.LABEL }, Data.CONTACT_ID
					+ "=?" + " AND " + Data.MIMETYPE + "='"
					+ Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.IS_PRIMARY
					+ " != 0", new String[] { user_id }, null);

			// Do something with phones
			if (pCur.getCount() > 0) {
				pCur.moveToFirst();
				pn = pCur
						.getString(pCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
				pCur.close();
			} else {

				Cursor pCur2 = cr.query(Data.CONTENT_URI, new String[] {
						Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Phone.CONTENT_ITEM_TYPE + "' AND "
								+ Phone.IS_PRIMARY + " != 1",
						new String[] { user_id }, null);

				if (pCur2.getCount() > 0) {
					pCur2.moveToFirst();
					pn = "many";
					many = true;
					/*
					 * Toast.makeText(getBaseContext(),
					 * pCur2.getColumnNames().toString(),
					 * Toast.LENGTH_SHORT).show();
					 */
					String[] allNums = new String[pCur2.getCount()];
					final String[] onlyNums = new String[pCur2.getCount()];
					int i = 0;
					do {
						String CurNum = pCur2
								.getString(pCur2
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						String CurNumType = pCur2
								.getString(pCur2
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
						/*
						 * Toast.makeText(getBaseContext(), CurNum,
						 * Toast.LENGTH_SHORT).show();
						 */
						String label = (String) Phone.getTypeLabel(
								getResources(), Integer.parseInt(CurNumType),
								"null");
						allNums[i] = label + " [" + CurNum + "]";
						onlyNums[i] = CurNum;
						i++;

					} while (pCur2.moveToNext());

					ListView numList = (ListView) findViewById(R.id.many_nums);
					TableLayout listL = (TableLayout) findViewById(R.id.listL);
					RelativeLayout boss = (RelativeLayout) findViewById(R.id.superP);
					boss.setOnClickListener(closeList);

					if (i != 0) {
						numList.setAdapter(new ArrayAdapter<String>(this,
								android.R.layout.simple_list_item_1, allNums));
						listL.bringToFront();
						listL.setVisibility(1);
						numList.setVisibility(1);
					}
					numList.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							// When clicked, show a toast with the TextView text
							findViewById(R.id.listL).setVisibility(View.GONE);
							/*
							 * Toast.makeText(getApplicationContext(),
							 * onlyNums[position], Toast.LENGTH_SHORT).show();
							 */
							Intent call_con = new Intent(Intent.ACTION_CALL);
							call_con.setData(Uri.parse("tel:"
									+ onlyNums[position]));
							startActivity(call_con);
						}
					});

				}
			}
		}
		/*
		 * Toast.makeText(getBaseContext(), pn+" clicked!",
		 * Toast.LENGTH_SHORT).show();
		 */
		if (pn != "null" && many == false) {
			Intent intent = new Intent(Intent.ACTION_CALL);
			intent.setData(Uri.parse("tel:" + pn));
			startActivity(intent);
			/*
			 * Toast.makeText(getBaseContext(), pn+"",
			 * Toast.LENGTH_LONG).show();
			 */
		} else if (pn == "null" && many == false) {
			Toast.makeText(getBaseContext(), "No number assigned for contact!",
					Toast.LENGTH_LONG).show();
		}
	}

	public void smsThis(String user_id) {
		String pn = "null";
		boolean many = false;
		ContentResolver cr = getContentResolver();

		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
				ContactsContract.Contacts._ID + " = ?",
				new String[] { user_id }, null);
		cur.moveToFirst();
		if (Integer.parseInt(cur.getString(cur
				.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
			Cursor pCur = cr.query(Data.CONTENT_URI, new String[] { Data._ID,
					Phone.NUMBER, Phone.TYPE, Phone.LABEL }, Data.CONTACT_ID
					+ "=?" + " AND " + Data.MIMETYPE + "='"
					+ Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.IS_PRIMARY
					+ " != 0", new String[] { user_id }, null);

			// Do something with phones
			if (pCur.getCount() > 0) {
				pCur.moveToFirst();
				pn = pCur
						.getString(pCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
				pCur.close();
			} else {

				Cursor pCur2 = cr.query(Data.CONTENT_URI, new String[] {
						Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Phone.CONTENT_ITEM_TYPE + "' AND "
								+ Phone.IS_PRIMARY + " != 1",
						new String[] { user_id }, null);

				if (pCur2.getCount() > 0) {
					pCur2.moveToFirst();
					pn = "many";
					many = true;
					/*
					 * Toast.makeText(getBaseContext(),
					 * pCur2.getColumnNames().toString(),
					 * Toast.LENGTH_SHORT).show();
					 */
					String[] allNums = new String[pCur2.getCount()];
					final String[] onlyNums = new String[pCur2.getCount()];
					int i = 0;
					do {
						String CurNum = pCur2
								.getString(pCur2
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						String CurNumType = pCur2
								.getString(pCur2
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
						/*
						 * Toast.makeText(getBaseContext(), CurNum,
						 * Toast.LENGTH_SHORT).show();
						 */
						String label = (String) Phone.getTypeLabel(
								getResources(), Integer.parseInt(CurNumType),
								"null");
						allNums[i] = label + " [" + CurNum + "]";
						onlyNums[i] = CurNum;
						i++;

					} while (pCur2.moveToNext());

					ListView numList = (ListView) findViewById(R.id.many_nums);
					TableLayout listL = (TableLayout) findViewById(R.id.listL);
					RelativeLayout boss = (RelativeLayout) findViewById(R.id.superP);
					boss.setOnClickListener(closeList);
					TextView listH = (TextView) findViewById(R.id.listHead);
					listH.setText("Send SMS to :");

					if (i != 0) {
						numList.setAdapter(new ArrayAdapter<String>(this,
								android.R.layout.simple_list_item_1, allNums));
						listL.bringToFront();
						listL.setVisibility(1);
						numList.setVisibility(1);
					}
					numList.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							// When clicked, show a toast with the TextView text
							findViewById(R.id.listL).setVisibility(View.GONE);
							/*
							 * Toast.makeText(getApplicationContext(),
							 * onlyNums[position], Toast.LENGTH_SHORT).show();
							 */
							Intent call_con = new Intent(Intent.ACTION_SENDTO);
							call_con.setData(Uri.parse("smsto:"
									+ onlyNums[position]));
							startActivity(call_con);
						}
					});

				}
			}
		}
		/*
		 * Toast.makeText(getBaseContext(), pn+" clicked!",
		 * Toast.LENGTH_SHORT).show();
		 */
		if (pn != "null" && many == false) {
			Intent intent = new Intent(Intent.ACTION_SENDTO);
			intent.setData(Uri.parse("smsto:" + pn));
			startActivity(intent);
			/*
			 * Toast.makeText(getBaseContext(), pn+"",
			 * Toast.LENGTH_LONG).show();
			 */
		} else if (pn == "null" && many == false) {
			Toast.makeText(getBaseContext(), "No number assigned for contact!",
					Toast.LENGTH_LONG).show();
		}
	}

	public void mmsThis(String user_id) {
		String pn = "null";
		boolean many = false;
		ContentResolver cr = getContentResolver();

		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
				ContactsContract.Contacts._ID + " = ?",
				new String[] { user_id }, null);
		cur.moveToFirst();
		if (Integer.parseInt(cur.getString(cur
				.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
			Cursor pCur = cr.query(Data.CONTENT_URI, new String[] { Data._ID,
					Phone.NUMBER, Phone.TYPE, Phone.LABEL }, Data.CONTACT_ID
					+ "=?" + " AND " + Data.MIMETYPE + "='"
					+ Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.IS_PRIMARY
					+ " != 0", new String[] { user_id }, null);

			// Do something with phones
			if (pCur.getCount() > 0) {
				pCur.moveToFirst();
				pn = pCur
						.getString(pCur
								.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
				pCur.close();
			} else {

				Cursor pCur2 = cr.query(Data.CONTENT_URI, new String[] {
						Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL },
						Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
								+ Phone.CONTENT_ITEM_TYPE + "' AND "
								+ Phone.IS_PRIMARY + " != 1",
						new String[] { user_id }, null);

				if (pCur2.getCount() > 0) {
					pCur2.moveToFirst();
					pn = "many";
					many = true;
					/*
					 * Toast.makeText(getBaseContext(),
					 * pCur2.getColumnNames().toString(),
					 * Toast.LENGTH_SHORT).show();
					 */
					String[] allNums = new String[pCur2.getCount()];
					final String[] onlyNums = new String[pCur2.getCount()];
					int i = 0;
					do {
						String CurNum = pCur2
								.getString(pCur2
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						String CurNumType = pCur2
								.getString(pCur2
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
						/*
						 * Toast.makeText(getBaseContext(), CurNum,
						 * Toast.LENGTH_SHORT).show();
						 */
						String label = (String) Phone.getTypeLabel(
								getResources(), Integer.parseInt(CurNumType),
								"null");
						allNums[i] = label + " [" + CurNum + "]";
						onlyNums[i] = CurNum;
						i++;

					} while (pCur2.moveToNext());

					ListView numList = (ListView) findViewById(R.id.many_nums);
					TableLayout listL = (TableLayout) findViewById(R.id.listL);
					RelativeLayout boss = (RelativeLayout) findViewById(R.id.superP);
					boss.setOnClickListener(closeList);
					TextView listH = (TextView) findViewById(R.id.listHead);
					listH.setText("Send MMS to :");

					if (i != 0) {
						numList.setAdapter(new ArrayAdapter<String>(this,
								android.R.layout.simple_list_item_1, allNums));
						listL.bringToFront();
						listL.setVisibility(1);
						numList.setVisibility(1);
					}
					numList.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							// When clicked, show a toast with the TextView text
							findViewById(R.id.listL).setVisibility(View.GONE);
							/*
							 * Toast.makeText(getApplicationContext(),
							 * onlyNums[position], Toast.LENGTH_SHORT).show();
							 */
							Intent call_con = new Intent(Intent.ACTION_SENDTO);
							call_con.setData(Uri.parse("mmsto:"
									+ onlyNums[position]));
							startActivity(call_con);
						}
					});

				}
			}
		}
		/*
		 * Toast.makeText(getBaseContext(), pn+" clicked!",
		 * Toast.LENGTH_SHORT).show();
		 */
		if (pn != "null" && many == false) {
			Intent intent = new Intent(Intent.ACTION_SENDTO);
			intent.setData(Uri.parse("mmsto:" + pn));
			startActivity(intent);
			/*
			 * Toast.makeText(getBaseContext(), pn+"",
			 * Toast.LENGTH_LONG).show();
			 */
		} else if (pn == "null" && many == false) {
			Toast.makeText(getBaseContext(), "No number assigned for contact!",
					Toast.LENGTH_LONG).show();
		}
	}

	private OnClickListener closeList = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			findViewById(R.id.listL).setVisibility(View.GONE);
		}
	};

	private OnClickListener add_user1 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user1 = settings.getBoolean("sd1", false);
			if (user1 == false) {
				onCreateDialog(0, "sd_data_1").show();
			} else {
				String uid = settings.getString("sd_data_1", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user2 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user2 = settings.getBoolean("sd2", false);
			if (user2 == false) {
				onCreateDialog(0, "sd_data_2").show();
			} else {
				String uid = settings.getString("sd_data_2", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user3 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user3 = settings.getBoolean("sd3", false);
			if (user3 == false) {
				onCreateDialog(0, "sd_data_3").show();
			} else {
				String uid = settings.getString("sd_data_3", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user4 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user4 = settings.getBoolean("sd4", false);
			if (user4 == false) {
				onCreateDialog(0, "sd_data_4").show();
			} else {
				String uid = settings.getString("sd_data_4", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user5 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user5 = settings.getBoolean("sd5", false);
			if (user5 == false) {
				onCreateDialog(0, "sd_data_5").show();
			} else {
				String uid = settings.getString("sd_data_5", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user6 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user6 = settings.getBoolean("sd6", false);
			if (user6 == false) {
				onCreateDialog(0, "sd_data_6").show();
			} else {
				String uid = settings.getString("sd_data_6", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user7 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user7 = settings.getBoolean("sd7", false);
			if (user7 == false) {
				onCreateDialog(0, "sd_data_7").show();
			} else {
				String uid = settings.getString("sd_data_7", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user8 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user8 = settings.getBoolean("sd8", false);
			if (user8 == false) {
				onCreateDialog(0, "sd_data_8").show();
			} else {
				String uid = settings.getString("sd_data_8", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
	private OnClickListener add_user9 = new OnClickListener() {
		public void onClick(View v) {
			// do something when the button is clicked
			SharedPreferences settings = getSharedPreferences(SD_DATA,
					Context.MODE_PRIVATE);
			user9 = settings.getBoolean("sd9", false);
			if (user9 == false) {
				onCreateDialog(0, "sd_data_9").show();
			} else {
				String uid = settings.getString("sd_data_9", "null");
				if (uid != "null") {
					callThis(uid);
				}
			}
		}
	};
}